/*
    SPDX-FileCopyrightText: 2026 Michail Vourlakos <mvourlakos@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <QtTest>

#include "../app/layout/addviewdecision.h"

using namespace Latte;
using namespace Latte::Layout;

static AddViewInputs base()
{
    AddViewInputs in;
    in.onPrimary = true;
    in.screenId = 0;
    in.screenIdValid = true;
    in.screenActive = true;
    in.visibilityMode = Types::DodgeActive;
    in.configByPassWM = false;
    return in;
}

class AddViewDecisionTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void primaryUsesPrimaryScreenNoReject();
    void nonPrimaryActiveScreenUsesExplicit();
    void nonPrimaryInactiveScreenIsRejected();
    void nonPrimaryInvalidScreenUsesPrimary();
    void alwaysVisibleForcesByPassWmFalse();
    void dodgeActiveHonoursConfigByPassWm();
};

void AddViewDecisionTest::primaryUsesPrimaryScreenNoReject()
{
    const AddViewDecision d = AddViewDecisionMaker::decide(base());
    QVERIFY(!d.reject);
    QVERIFY(!d.useExplicitScreen);
}

void AddViewDecisionTest::nonPrimaryActiveScreenUsesExplicit()
{
    AddViewInputs in = base();
    in.onPrimary = false;
    in.screenId = 3;
    in.screenActive = true;
    const AddViewDecision d = AddViewDecisionMaker::decide(in);
    QVERIFY(!d.reject);
    QVERIFY(d.useExplicitScreen);
}

void AddViewDecisionTest::nonPrimaryInactiveScreenIsRejected()
{
    AddViewInputs in = base();
    in.onPrimary = false;
    in.screenId = 3;
    in.screenActive = false;
    const AddViewDecision d = AddViewDecisionMaker::decide(in);
    QVERIFY(d.reject);
}

void AddViewDecisionTest::nonPrimaryInvalidScreenUsesPrimary()
{
    AddViewInputs in = base();
    in.onPrimary = false;
    in.screenIdValid = false;       // invalid screen id -> falls through to primary
    in.screenActive = false;
    const AddViewDecision d = AddViewDecisionMaker::decide(in);
    QVERIFY(!d.reject);
    QVERIFY(!d.useExplicitScreen);
}

void AddViewDecisionTest::alwaysVisibleForcesByPassWmFalse()
{
    //! the whole always-visible family forces byPassWM false regardless of the config value
    const QList<Types::Visibility> family{Types::AlwaysVisible, Types::WindowsGoBelow,
                                          Types::WindowsCanCover, Types::WindowsAlwaysCover};
    for (const Types::Visibility mode : family) {
        AddViewInputs in = base();
        in.visibilityMode = mode;
        in.configByPassWM = true;
        const AddViewDecision d = AddViewDecisionMaker::decide(in);
        QVERIFY2(!d.byPassWM, "always-visible family must override byPassWM to false");
    }
}

void AddViewDecisionTest::dodgeActiveHonoursConfigByPassWm()
{
    AddViewInputs in = base();
    in.visibilityMode = Types::DodgeActive;
    in.configByPassWM = true;
    const AddViewDecision d = AddViewDecisionMaker::decide(in);
    QVERIFY(d.byPassWM);
}

QTEST_MAIN(AddViewDecisionTest)
#include "addviewdecisiontest.moc"
